<template>
  <div>
    <el-dialog title="内镜签到" width="500px" :visible.sync="signUpVisible" :close-on-click-modal="true" :destroy-on-close="true">
      <el-form label-width="60px" :model="formdata" ref="formdata" @submit.native.prevent>
        <el-form-item label="刷卡处">
          <el-input
            id="signInput"
            ref="signInput"
            size="small"
            style="text-align:left"
            v-model="formdata.searchCommonValue"
            clearable
            placeholder="请输入"
            @keyup.enter.native="search"
          >
          </el-input>
          <div class="ky-inlineblock">
            <WSCardReader ref="WSCardReader" btn-size="mini" from="sign" @getWSCardReaderMsg="getWSCardReaderMsg"/>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import WSCardReader from '@/components/WSCardReaderNew/index'
import { dateFormatter } from '@/utils/validate'
import { getKyReaderCardOptions, debounce } from '@/utils/utils'
import rtUtil from '@/components/WSCardReaderNew/readerType'
// import { getadvancedpages, postsigninorder } from '@/api/api.js'
// import bus from '@/api/bus'
export default {
  components: { WSCardReader },
  props: {
    hospitalCode: {
      type: String,
      default: ''
    },
    appointType: {
      type: String,
      default: ''
    }
  },
  computed: {
    appConfig() {
      return this.$store.state.settings.appConfig
    },
    KYCARDREADER_OPTIONS() {
      // 1234567890123456789012345678
      let arr = ((this.appConfig && this.appConfig.baseConfig && this.appConfig.baseConfig.kyReaderCardNameArr && this.appConfig.baseConfig.kyReaderCardNameArr.length) && this.appConfig.baseConfig.kyReaderCardNameArr) || []
      console.log('KYCARDREADER_OPTIONS=>arr', arr)
      let options = getKyReaderCardOptions(arr)
      console.log('KYCARDREADER_OPTIONS=>options', options)
      return (options.length && options) || window.g.KYCARDREADER_OPTIONS || window.g.KYCARDREADER_OPTIONS_FUDING || rtUtil.defaultType
    }
  },
  watch: {
    signUpVisible(val) {
      if (val) {
        this.formdata.searchCommonValue = ''
        this.$nextTick(() => {
          this.$refs.signInput && this.$refs.signInput.focus()
          // 创建键盘模拟输入的事件监听
          this.$refs.WSCardReader.addInputEventListener('signInput', () => {
            return this.formdata.searchCommonValue.trim()
          })
          // setTimeout(() => {
          // 监听键盘事件
          // document.getElementById('signInput').addEventListener('keydown', (event) => {
          //   // // console.log(`按下的keyCode:${event.keyCode}, 按下的key:${event.key}`)
          //   // 根据不同的键执行不同的操作
          //   if (event.keyCode == 13 || event.key == 'Enter') {
          //     let qText = this.formdata.searchCommonValue.trim()
          //     console.warn('qText', qText)
          //     if (qText == '*=-*' || qText.length == 28 || qText.length == 117 || (qText.length == 68 || qText.length == 116) || qText.indexOf(':') > -1) {
          //       // 市民卡先刷出的是*=-*
          //       this.$refs.WSCardReader.wsChange('', qText)
          //     }
          //   }
          // })
          // }, 1000)
        })
      } else {
        // bus.$off('WSCardReader_msg_sign')
      }
    }
  },
  data() {
    return {
      applyInfoHospital: window.g.applyInfoHospital || '',
      // kyCardReaderOptions: '',
      signUpVisible: false,
      formdata: {
        searchCommonValue: ''
      },
      accessNoList: []
    }
  },
  mounted() {
    // this.kyCardReaderOptions = this.KYCARDREADER_OPTIONS
    // console.log('KYCARDREADER_OPTIONS', this.KYCARDREADER_OPTIONS)
    // bus.$on('WSCardReader_msg_sign', (res) => {
    //   console.warn('WSCardReader_msg_sign=>res', res)
    //   this.formdata.searchCommonValue = res.searchText
    //   // this.search()
    //   this.$nextTick(() => {
    //     this.search()
    //   }, 200)
    // })
    // setTimeout(() => {
    //   let params = {
    //     searchWay: '身份证号',
    //     searchText: '330327199207250810'
    //   }
    //   bus.$emit('WSCardReader_msg_sign', params)
    // }, 1000)
  },
  methods: {
    // 刷卡返回结果的处理
    getWSCardReaderMsg(res) {
      // this.searchValue = res.searchWay
      if (res.searchText) {
        this.formdata.searchCommonValue = res.searchText
        this.$nextTick(() => {
          this.search()
        }, 200)
      } else {
        this.formdata.searchCommonValue = ''
      }
    },
    // doCardInit() {
    //   if (this.kyCardReaderOptions && this.kyCardReaderOptions.length > 0) {
    //     this.kyCardReaderOptions[1].data.CardId = this.formdata.searchCommonValue
    //     // localStorage.setItem('yjyy-CardId', this.searchCommonValue)
    //     this.$store.dispatch('patient/setCardId', this.formdata.searchCommonValue)
    //     this.$refs.WSCardReader.readerCardOption = this.kyCardReaderOptions
    //   } else {
    //     alert('请先在config.js中配置读卡器参数')
    //   }
    //   this.$refs.WSCardReader.readerType = 'sbjzfds-fj' // 磁条卡
    //   this.$refs.WSCardReader.wsInit()
    // },
    search() {
      let qText = this.formdata.searchCommonValue.trim()
      if (qText) {
        this.$refs.WSCardReader.searchJudge(qText, (res) => {
          console.log('进入回调', res)
          // this.searchValue = res.searchWay
          this.doSearch()
        }, (res) => {
          console.log('进入回调, 但不查询', res)
          // this.searchValue = res.searchWay
        })
        // if (this.applyInfoHospital == 'fuding') {
        //   if (qText == '*=-*') {
        //     // 市民卡先刷出的是*=-*
        //   } else if (qText.length == 28) {
        //     // 电子医保凭证28位
        //   } else if (qText.length == 9) {
        //     // 市民卡9位-福鼎市医院
        //     // console.log('this.doSearch()=>search()=>qText.length == 9')
        //     this.doSearch()
        //   } else if (qText.length == 33) {
        //     // 门诊卡33位,刷出结果10位
        //     this.doCardInit()
        //   } else {
        //     // console.log('this.doSearch()=>search()=>else1')
        //     this.doSearch()
        //   }
        // } else {
        //   if (qText == '*=-*') {
        //     // 市民卡先刷出的是*=-*
        //   } else if (qText.length == 28) {
        //     // 医保凭证刷出的是28位 A07990CBE30698D1CF7FD97000B0
        //   } else if (qText.length == 117) {
        //     // 健康卡刷出的是117位 A07990CBE30698D1CF7FD97000B05078FCA0BC363754F528E0E172D771D359F7:0:E7B24F8B09C3EB50A98681899395F7D1:3300A0001APPA0015
        //     // this.searchValue = '病人ID' // 病人ID
        //   } else if (qText.length == 68 || qText.length == 116) {
        //     // 医院里的就医助手二维码刷出的是68或116位
        //     // this.searchValue = '病人ID' // 病人ID
        //   } else if (qText.indexOf(':') > -1) {
        //     // 健康码中有":"
        //     // this.searchValue = '卡号' // 卡号
        //   } else {
        //     this.doSearch()
        //   }
        // }
      } else {
        // this.doSearch()
        // // console.log('this.doSearch()=>search()=>else3')
        debounce(() => {
          this.doSearch()
        }, 1000)
      }
    },
    doSearch() {
      let params = {
        StartDate: dateFormatter(new Date(new Date().getTime()), false),
        EndDate: dateFormatter(new Date(new Date().getTime()), false),
        signNo: this.formdata.searchCommonValue.trim(),
        scheduledDep: ['内窥镜'],
        checkStatus: ['未检查']
      }
      console.warn('search=>params', params)
      this.$emit('getSignInfo', params)
    }
    // wsChange() {
    //   this.$refs.signInput && this.$refs.signInput.focus()
    // }
  }
}
</script>
